<template>
  <el-dialog title="分配用户" :visible.sync="visible" width="80%" top="5vh" append-to-body>
    <SearchWithOperate
      :form-options="formOptions"
      btn-items="add"
      @onSearch="handleQuery"
      @onAdd="openSelectUser"
    >
      <template v-slot:otherBtn>
        <el-tooltip :open-delay="600" content="批量取消授权" placement="bottom" class="operate">
          <el-button
            v-hasPermi="['system:role:remove']"
            class="operate"
            type="primary"
            plain
            size="mini"
            icon="iconfont icon-opt-auth-cancel"
            @click="cancelAuthUserAll"
          />
        </el-tooltip>
      </template>
    </SearchWithOperate>

    <el-table v-loading="loading" :data="userList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" />
      <el-table-column label="用户名称" prop="userName" show-overflow-tooltip />
      <el-table-column label="用户姓名" prop="nickName" show-overflow-tooltip />
      <el-table-column label="邮箱" prop="email" show-overflow-tooltip />
      <el-table-column label="手机" prop="phonenumber" show-overflow-tooltip />
      <el-table-column label="部门" prop="dept.deptName" show-overflow-tooltip />
      <el-table-column label="状态" align="center" width="70" prop="status">
        <template slot-scope="scope">
          <span class="stateShell">
            <div v-if="scope.row.status==='0'" class="common-prompt" style="background-color:#67C23A" />
            <div v-else class="common-prompt" style="background-color:#F56C6C" />
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
          </span></template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:role:remove']"
            size="mini"
            type="primary"
            plain
            @click="cancelAuthUser(scope.row)"
          >取消授权</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <select-user ref="select" :post-id="queryParams.postId" @ok="handleQuery" />
  </el-dialog>
</template>

<script>
import { getPostUsers } from '@/api/system/user'
import { cancelAll, cancelOne } from '@/api/system/post'
import selectUser from './SelectUser'

export default {
  name: 'AuthUser',
  dicts: ['sys_normal_disable'],
  components: { selectUser },
  data() {
    return {
      formOptions: [
        {
          label: '用户姓名',
          prop: 'nickName',
          type: 'input',
          placeholder: '请输入用户姓名'
        }, {
          label: '手机号码',
          prop: 'phoneNumber',
          type: 'input',
          placeholder: '请输入手机号码'
        }
      ],
      // 遮罩层
      loading: true,
      // 选中用户组
      userIds: [],
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postId: 0,
        nickName: undefined,
        phoneNumber: undefined
      },
      visible: false
    }
  },
  methods: {
    show(postId) {
      this.resetForm('queryForm')
      this.queryParams.postId = postId
      this.visible = true
      this.getList()
    },
    /** 查询授权用户列表 */
    getList() {
      this.loading = true
      getPostUsers(this.queryParams).then(response => {
        this.userList = response.rows
        this.total = response.total
        this.loading = false
      }
      )
    },
    // 返回按钮
    handleClose() {
      const obj = { path: '/system/post' }
      this.$tab.closePage(obj)
    },
    /** 搜索按钮操作 */
    handleQuery(param) {
      if (param) {
        this.queryParams = Object.assign(this.queryParams, param)
      }
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.userIds = selection.map(item => {
        const dept = item.dept || {}
        return { userId: item.userId, deptId: dept.deptId, postId: this.queryParams.postId }
      })
      this.multiple = !selection.length
    },
    /** 打开授权用户表弹窗 */
    openSelectUser() {
      this.$refs.select.show()
    },
    /** 取消授权按钮操作 */
    cancelAuthUser(row) {
      const postId = this.queryParams.postId
      const dept = row.dept || {}
      this.$modal.confirm('确认要取消该用户名为："' + row.userName + '"部门为："' + row.dept.deptName + '"岗位吗？').then(function() {
        return cancelOne({ userId: row.userId, postId: postId, deptId: dept.deptId || '' })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('取消授权成功')
      }).catch(() => {})
    },
    /** 批量取消授权按钮操作 */
    cancelAuthUserAll() {
      if (this.userIds.length > 0) {
        this.$modal.confirm('是否取消选中用户授权数据项？').then(() => {
          return cancelAll(this.userIds).then(() => {
            this.getList()
            this.$modal.msgSuccess('取消授权成功')
          })
        }).catch(() => {})
      } else {
        this.$modal.msgWarning('未选中数据')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
